package com.sq.partner.manager.util;

import com.google.gson.Gson;

import java.io.Reader;

import org.apache.log4j.Logger;
import org.springframework.util.Assert;

/**
 * json对像处理
 * @author sankun
 *
 */
public class JsonUtils {
	private static Logger logger = LogUtil.getLog(JsonUtils.class);
    private static Gson gson = new Gson();
    /**
     * 对象转换为json字符串
     * @param obj
     * @return json
     */
    public static String toJson(Object obj) {
    	try {
    		return gson.toJson(obj);
		} catch (Exception e) {
			logger.error(e);
		}
    	return null;
    }

    public static <T> T parseJson(Reader json, Class<T> tClass) {
        return gson.fromJson(json, tClass);
    }
    /**
     * 将json 字符串转换为对象
     * @param json
     * @param tClass
     * @return
     */
    public static <T> T parseJson(String json, Class<T> tClass) {
    	Assert.hasText(json);
		Assert.notNull(tClass);
		try {
			return gson.fromJson(json, tClass);
		} catch (Exception e) {
			logger.error(e);
		}
		return null;
    }
}
